package com.gking.resourcePool.controller;


import com.gking.resourcePool.common.Result;
import com.gking.resourcePool.entity.Project;
import com.gking.resourcePool.service.Project.ProjectService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/project")
public class ProjectController {

    @Autowired
    private ProjectService projectService;


    /**
     * 新增项目
     *
     * @param project 项目相关信息
     * @return s
     */
    @RequiresPermissions("RBS:project:add")
    @PostMapping("/add")
    public Result<String> addProject(@RequestBody Project project) {
        String name = project.getName();
        String description = project.getDescription();

        if (name.isEmpty()) return Result.error("项目名不能为空");
        try {
            return projectService.addProject(name, description);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 修改项目
     *
     * @param project 项目相关信息
     * @return s
     */
    @RequiresPermissions("RBS:project:update")
    @PostMapping("/update")
    public Result<String> updateProject(@RequestBody Project project) {
        String id = project.getId();
        String name = project.getName();
        String description = project.getDescription();

        if (id.isEmpty() || name.isEmpty()) return Result.error("参数不完整");
        try {
            return projectService.updateProject(id, name, description);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除项目
     *
     * @param projectId 项目id
     * @return s
     */
    @RequiresPermissions("RBS:project:delete")
    @PostMapping("/delete")
    public Result<String> deleteProject(@RequestParam String projectId) {
        if (projectId.isEmpty()) return Result.error("项目id不能为空");
        try {
            return projectService.deleteProject(projectId);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取项目列表
     *
     * @return l
     */
    @RequiresPermissions("RBS:project:get")
    @GetMapping("/get")
    public Result<List<Project>> getProjects() {
        try {
            return projectService.getProjects();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }


}
